﻿using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using FanaticRock.Core.Extensions;
using FanaticRock.MediaSource.BilibiliAPI.RequestVo.Fav.Folder.Created;

namespace FanaticRock.ViewModels
{
    public partial class MainViewModel
    {
        #region props

        [ObservableProperty]
        private ObservableCollection<AlbumViewModel> _albums = new ObservableCollection<AlbumViewModel>();

        [ObservableProperty]
        private LazyQuerySongsViewModel _lazyQuerySongsViewModel;

        #endregion

        #region commands

        [RelayCommand]
        public async void GetAlbums()
        {
            var sessData = await _bilibiliCookieService.GetSESSDATA();
            var mid = await _bilibiliCookieService.GetMId();

            var vo = new ListAllRequestVo(sessData, mid);
            var dto = await MediaSource.BilibiliAPI.API.Fav.Folder.Created.ListAll.Invoke(vo);
            if (dto.Code != MediaSource.BilibiliAPI.Enums.CodeEnum.Success)
            {
                this.Info($"MainViewModel.GetAlbums: 获得B站收藏夹请求失败 {dto.Message}");
                return;
            }



            var newAlbums = new List<AlbumViewModel>();
            foreach (var album in dto.Data.List)
            {
                var id = album.Id;

                var infoVo = new FanaticRock.MediaSource.BilibiliAPI.RequestVo.Fav.Folder.InfoRequestVo(id);
                var infoDto = await MediaSource.BilibiliAPI.API.Fav.Folder.Info.Invoke(infoVo);

                var newAlbum = new AlbumViewModel(infoDto);
                newAlbums.Add(newAlbum);
            }

            Albums.Clear();
            Albums = new ObservableCollection<AlbumViewModel>(newAlbums);
        }

        ///<summary>传入b站收藏夹中，</summary>
        public async Task CreateAlbumSongs(long id)
        {
            LazyQuerySongsViewModel?.Dispose();

            var lazyQuerySongsViewModel = Ioc.Default.GetService<LazyQuerySongsViewModel>();

            await lazyQuerySongsViewModel.Init(id);

            LazyQuerySongsViewModel = lazyQuerySongsViewModel;
        }

        #endregion
    }
}
